<?php
require_once('includes/connexion_bdd.php');
require_once('includes/fonctions_donnees.php');

class reservation  {

	protected $id;
	protected $idClient;
	protected $idMateriel;
	protected $dateDebut;
	protected $dateFin;
	protected $prenom;
	protected $duree;

	protected $valid;
	protected $erreurs;


	public function __construct(){
		$num_args = func_num_args();

		switch($num_args){
			case 0 :
				$bool =true;
				break;
			case 5 :
				$bool = $this->setIdClient(func_get_arg(0));
				$bool &=$this->setIdMateriel(func_get_arg(1));
				$bool &=$this->setDateDebut(func_get_arg(2));
				$bool &=$this->setDateFin(func_get_arg(3));
				$bool &=$this->setDuree(func_get_arg(2),func_get_arg(3));
				$bool &=$this->setPrenom(func_get_arg(4));
				break;
			default :
				$bool=false;
			$this->erreurs.="Param&egrave;tres de construction reservation invalides; <br />";
			break;
		}
		$this->valid = $bool ;

	}

	//////////////////////
	//Getters et setters//
	//////////////////////

	public function getId(){
		return $this->id;
	}

	public function getIdClient(){
		return $this->idClient;
	}

	public function setIdClient($id){
		if(!empty($id)&& is_numeric($id)){
			$this->idClient= clean_data($id);
			return 1;
		}
		else {
			$this->erreurs.="Vos informations client sont introuvables <br />";
			return 0;
		}
	}

	public function getIdMateriel(){
		return affichage($this->idMateriel);
	}

	public function setIdMateriel($id){
		if(!empty($id)&& is_numeric($id)&&$id!=null){
			$this->idMateriel= clean_data($id);
			return 1;
		}
		else {
			$this->erreurs.="Le mat&eacute;riel demand&eacute; n'est pas disponible <br />";
			return 0;
		}
	}

	public function getDateDebut(){
		$date = affichage($this->dateDebut);
		$date = preg_replace('#^(201[1-2])-([0-1][0-9])-([0-3]?[0-9])$#', '$3/$2/$1', $date);
		return $date;
	}

	public function setDateDebut($date){
		if(!empty($date)&& preg_match( "#^[0-3][0-9][\.:\-/]?[0-1][0-9][\.:\-/]?201[1-2]$#",$date)){
			$date = preg_replace('#^([0-3]?[0-9])[\.:\-/]?([0-1][0-9])[\.:\-/]?(201[1-2])$#', '$3-$2-$1', $date);
			$this->dateDebut = clean_data($date);
			return 1;
		}
		else {
			$this->erreurs.="La date de d&eacute;but est incorrecte<br />";
			return 0;
		}
	}

	public function getDateFin(){
		$date = affichage($this->dateFin);
		$date = preg_replace('#^(201[1-2])-([0-1][0-9])-([0-3]?[0-9])$#', '$3/$2/$1', $date);
		return $date;
	}

	public function setDateFin($date){
		if(!empty($date)&& preg_match( "#^[0-3][0-9][\.:\-/]?[0-1][0-9][\.:\-/]?201[1-2]$#",$date)){
			$date = preg_replace('#^([0-3]?[0-9])[\.:\-/]?([0-1][0-9])[\.:\-/]?(201[1-2])$#', '$3-$2-$1', $date);
			$this->dateFin = clean_data($date);
			return 1;
		}
		else {
			$this->erreurs.="La date de fin est incorrecte<br />";
			return 0;
		}
	}
	public function getDuree(){
		$date = affichage($this->duree);
		return $date;
	}
	public function setDuree($dateD, $dateF){
		if(!empty($dateD) && !empty($dateF)){
			
			$this->duree = clean_data($dateF-$dateD+1);
			return 1;
		}
		else {
			return 0;
		}
	}

	public function getPrenom(){
		return affichage($this->prenom);
	}

	public function setPrenom($prenom) {
		if(!empty($prenom)){
			$this->prenom = clean_data($prenom);
		}
		return 1;
	}


	public function isValid(){
		return $this->valid;
	}

	public function getErreurs(){
		return $this->erreurs;
	}

	/////////////////////////////
	//Fonctions d'acces a la BD//
	/////////////////////////////

	public function ajoutBD() {

		try {
			$bdd = Connect();

			$req = $bdd->prepare('INSERT INTO reservation(idClient, idMateriel, dateDebut, dateFin, prenom)
												VALUES(:idClient, :idMateriel, :dateDebut, :dateFin, :prenom)');
			$req->execute(array(
							'idClient' => $this->idClient,
							'idMateriel' => $this->idMateriel,
							'dateDebut' => $this->dateDebut,
							'dateFin' => $this->dateFin,
							'prenom' => $this->prenom
			));

			$this->id = $bdd->lastInsertId();
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}

	public function modifBD($idR) {

		try {
			$bdd = Connect();
				
			$req = $bdd->prepare('UPDATE reservation SET idClient = :idClient,
													idMateriel = :idMateriel,
													dateDebut = :dateDebut,
													dateFin = :dateFin,
													prenom = :prenom,
													WHERE id=:id');
			$req->execute(array(
								'idClient' => $this->idClient,
								'idMateriel' => $this->idMateriel,
								'dateDebut' => $this->dateDebut,
								'dateFin' => $this->dateFin,
								'prenom' => $this->prenom,
								'id' => $idR
			));
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}

// 	renvoie toutes les reservations effectuees par un client donne
// 	pour la page mesReservations
	public static function getReservationsClient($idC) {

		try {
			$bdd = Connect();
				
			$req = $bdd->prepare('SELECT prenom, type, niveau, marque, modele, taille,
									DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
									DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
									FROM reservation r 
									INNER JOIN materiel m ON r.idMateriel =m.id
									INNER JOIN categorie c ON m.idCategorie = c.id
								 	WHERE idClient = ?
									ORDER BY r.dateDebut DESC
								');
			$req->execute(array($idC));
				
			return $req;
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}
	
	// 	renvoie toutes les reservations selon leur statut en cours, a venir ou terminees triees par date
	// 	pour la page reservations de l'admin
	public static function getAllReservations($statut) {
	
		try {
			$bdd = Connect();
			if($statut=="encours") {
	
				$req = $bdd->prepare('SELECT idClient, prenom, type, niveau, marque, modele, taille,
											DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
											DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
											FROM reservation r 
											INNER JOIN materiel m ON r.idMateriel =m.id
											INNER JOIN categorie c ON m.idCategorie = c.id
											WHERE dateFin>=DATE_FORMAT(now(),\'%Y-%m-%d\')
											AND dateDebut<=DATE_FORMAT(now(),\'%Y-%m-%d\')
											ORDER BY r.dateDebut DESC
										');
			}
			else if($statut=="avenir") {
				$req = $bdd->prepare('SELECT idClient, prenom, type, niveau, marque, modele, taille,
														DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
														DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
														FROM reservation r 
														INNER JOIN materiel m ON r.idMateriel =m.id
														INNER JOIN categorie c ON m.idCategorie = c.id
														WHERE dateDebut>=DATE_FORMAT(now(),\'%Y-%m-%d\')
														ORDER BY r.dateDebut DESC
													');
			}
			else if($statut=="terminees") {
				$req = $bdd->prepare('SELECT idClient, prenom, type, niveau, marque, modele, taille,
														DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
														DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
														FROM reservation r 
														INNER JOIN materiel m ON r.idMateriel =m.id
														INNER JOIN categorie c ON m.idCategorie = c.id
														WHERE dateFin<=DATE_FORMAT(now(),\'%Y-%m-%d\')
														ORDER BY r.dateDebut DESC
													');
			}
			$req->execute();
	
			return $req;
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
	}	

	public static function getReservationsMateriel($idM) {
	
		$ret = false;
	
		try {
			$bdd = Connect();
	
			$req = $bdd->prepare('SELECT * FROM reservation
										WHERE idMateriel = ?
									 	AND dateDebut>=DATE_FORMAT(now(),\'%Y-%m-%d\')
									');
			$req->execute(array($idM));
	
			if ($donnees = $req->fetch()) {
				$ret = true;
			}
			$req->closeCursor();
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
		return $ret;
	}
	
	public static function recherche($mot) {
		$resultats=null;
		try {
			$bdd = Connect();
			foreach (array('encours','avenir','terminees') as $statut) {
		
				if($statut=="encours") {
		
					$req = $bdd->prepare('SELECT idClient, r.prenom, type, c.niveau, marque, modele, taille,
												DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
												DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
												FROM reservation r 
												INNER JOIN materiel m ON r.idMateriel =m.id
												INNER JOIN categorie c ON m.idCategorie = c.id
												INNER JOIN client u ON r.idClient = u.id
												WHERE dateFin>=DATE_FORMAT(now(),\'%Y-%m-%d\')
												AND dateDebut<=DATE_FORMAT(now(),\'%Y-%m-%d\')
												AND (marque LIKE :debut
													OR modele LIKE :debut
													OR c.niveau LIKE :debut
													OR type LIKE :debut
													OR nom LIKE :debut
													OR u.prenom LIKE :debut
													OR r.prenom LIKE :debut
													OR sexe LIKE :debut
													OR email LIKE :debut)
												ORDER BY r.dateDebut DESC
											');
				}
				else if($statut=="avenir") {
					$req = $bdd->prepare('SELECT idClient, r.prenom, type, c.niveau, marque, modele, taille,
															DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
															DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
															FROM reservation r 
															INNER JOIN materiel m ON r.idMateriel =m.id
															INNER JOIN categorie c ON m.idCategorie = c.id
															INNER JOIN client u ON r.idClient = u.id
															WHERE dateDebut>=DATE_FORMAT(now(),\'%Y-%m-%d\')
															AND (marque LIKE :debut
																OR modele LIKE :debut
																OR c.niveau LIKE :debut
																OR type LIKE :debut
																OR nom LIKE :debut
																OR u.prenom LIKE :debut
																OR r.prenom LIKE :debut
																OR sexe LIKE :debut
																OR email LIKE :debut)
															ORDER BY r.dateDebut DESC
														');
				}
				else if($statut=="terminees") {
					$req = $bdd->prepare('SELECT idClient, r.prenom, type, c.niveau, marque, modele, taille,
															DATE_FORMAT(dateDebut, \'%d/%m/%Y\') AS dateDebut,
															DATE_FORMAT(dateFin, \'%d/%m/%Y\') AS dateFin
															FROM reservation r 
															INNER JOIN materiel m ON r.idMateriel =m.id
															INNER JOIN categorie c ON m.idCategorie = c.id
															INNER JOIN client u ON r.idClient = u.id
															WHERE dateFin<=DATE_FORMAT(now(),\'%Y-%m-%d\')
															AND (marque LIKE :debut
																OR modele LIKE :debut
																OR c.niveau LIKE :debut
																OR type LIKE :debut
																OR nom LIKE :debut
																OR u.prenom LIKE :debut
																OR r.prenom LIKE :debut
																OR sexe LIKE :debut
																OR email LIKE :debut)
															ORDER BY r.dateDebut DESC
														');
				}
				$req->execute(array('debut' => '%'.$mot. '%'));
				$resultats[$statut] = $req;
			}
		}
		catch(Exception $e){
			die('Erreur : '.$e->getMessage());
		}
		return $resultats;
	}
}
?>